package models

import (
	"strings"
	"time"
	"xorm.io/builder"
)

type Securityguard struct {
	Id               int       `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	UserId           int       `xorm:"not null comment('用户id外键关联dts_user中的id') index INT(11)"`
	SchoolId         int       `xorm:"not null comment('驾校id外键关联dts_school中的自增id') index INT(11)"`
	IcId             int       `xorm:"default 0 comment('IC卡id,安全员可以没有IC卡也可以拥有多张IC卡，当前为生效IC卡id') INT(11)"`
	FingerId         int       `xorm:"default 0 comment('指纹id，安全员可以拥有多个指纹记录，当前为生效指纹id') INT(11)"`
	PortraitId       int       `xorm:"default 0 comment('头像id，安全员可以拥有多个头像记录，当前为生效头像id') INT(11)"`
	LicenceNumber    string    `form:"licence_number" xorm:"default '' comment('驾驶证号') VARCHAR(128)"`
	LicenceGrantDate time.Time `xorm:"comment('驾驶证初领日期 YYYYMMDD') DATE"`
	OccupationNumber string    `form:"occupation_number" xorm:"default '' comment('教练员职业资格证件号') VARCHAR(128)"`
	OccupationRank   string    `form:"occupation_rank" xorm:"default '1' comment('教练员职业资格等级，1：一级，2：二级，3：三级，4：四级') ENUM('1','2','3','4')"`
	DrivingCarType   string    `form:"driving_car_type" xorm:"default 'C1' comment('准驾车型') ENUM('A1','A2','A3','B1','B2','C1','C2','C3','C4','C5','D','E','F','M','N','P')"`
	TeachingCarType  string    `form:"teaching_car_type" xorm:"default 'C1' comment('准教车型') ENUM('A1','A2','A3','B1','B2','C1','C2','C3','C4','C5','D','E','F','M','N','P')"`
	EntryDate        time.Time `xorm:"comment('入职日期 YYYYMMDD') DATE"`
	DepartureDate    time.Time `xorm:"comment('离职日期 YYYYMMDD') DATE"`
	Status           string    `form:"status" xorm:"default '1' comment('0：在职, 1：离职') ENUM('0','1')"`
	ApprovalStatus   string    `xorm:"default '0' comment('安全员提交运管审核状态，0:待审核，1:通过审核，2:未通过审核') ENUM('0','1','2')"`
	RecordStatus     string    `xorm:"not null default '0' comment('备案状态') ENUM('0','1')"`
	InstitutionCode  string    `xorm:"VARCHAR(16)"`
	Remark           string    `xorm:"VARCHAR(255)"`
	Isused           string    `xorm:"default '1' comment('1 在使用  0 已删除') ENUM('0','1')"`
}

type SignSecurityInfo struct {
	Id               int
	SchoolId         int
	UserName         string
	Sex              string
	Nation           string
	Phone            string
	Address          string
	IdNumber         string
	LicenceNumber    string
	LicenceGrantDate string
	OccupationNumber string
	OccupationRank   string
	DrivingCarType   string
	TeachingCarType  string
	Status           string
	EntryDate        string
	DepartureDate    string
	Imgurl           string
	Portraitid       int
}

type SecurityInfo struct {
	Id               int    `json:"id"`
	SchoolId         int    `json:"school_id"`
	Name             string `json:"name"`
	EntryDate        string `json:"entry_date"`
	DepartureDate    string `json:"departure_date"`
	Phone            string `json:"phone"`
	TeachingCarType  string `json:"teaching_car_type"`
	Status           string `json:"status"`
	ApprovalStatus   string `json:"approval_status"`
	RecordStatus     string `json:"record_status"`
	Nation           string `json:"nation"`
	Nationality      string `json:"nationality"`
	Address          string `json:"address"`
	IdNumber         string `json:"id_number"`
	LicenceNumber    string `json:"licence_number"`
	LicenceGrantDate string `json:"licence_grant_date"`
	OccupationNumber string `json:"occupation_number"`
	OccupationRank   string `json:"occupation_rank"`
	DrivingCarType   string `json:"driving_car_type"`
	ImageUrl         string `json:"image_url"`
	Native           string `json:"native"`
	PortraitCode     string `json:"portrait_code"`
	Sex              string `json:"sex"`
	IdType           string `json:"id_type"`
	District         string `json:"district"`
}

func (c *Securityguard) GetSecurityList(school_id string, filter builder.Cond, pagesize int, offset int) (list []*SecurityInfo, count int64, err error) {
	if err = engine.Table("dts_securityguard").Alias("c").Select("c.id,c.school_id,c.status,c.approval_status,c.record_status,c.teaching_car_type,c.entry_date,c.departure_date,u.name,u.phone,u.sex,u.address,u.id_number,u.id_type,u.district,u.native,u.Nation,u.Nationality,c.licence_number,c.licence_grant_date,c.occupation_number,c.occupation_rank,c.driving_car_type,dp.image_url,dp.institution_code as portrait_code").Join("LEFT", []string{"dts_user", "u"}, "c.user_id=u.id").Join("RIGHT", []string{"dts_portrait", "dp"}, "dp.id=c.portrait_id").Where("c.school_id=? and c.isused=?", school_id, "1").And(filter).Desc("c.id").Limit(pagesize, offset).Find(&list); err != nil {
		return list, 0, err
	}
	_, err = engine.Table("dts_securityguard").Alias("c").Select("count(c.id) as count").Join("LEFT", []string{"dts_user", "u"}, "c.user_id=u.id").Where("c.school_id=? and c.isused=?", school_id, "1").And(filter).Get(&count)
	if err != nil {
		return list, count, err
	}
	for k, _ := range list {
		list[k].Phone = strings.Replace(list[k].Phone, "s", "", -1)
		list[k].IdNumber = strings.Replace(list[k].IdNumber, "s", "", -1)
	}
	return list, count, nil
}
